//
//  HKMainPageCell.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/13.
//

#import "HKMainPageCell.h"
#import "HKCommonDefine.h"
#import "HKUITool.h"

static const CGFloat kKSTopMargin = 38.f;
static const CGFloat kKSBottomMargin = 5.f;
static const CGFloat kKSContainerH = 134.f;
static const CGFloat kKSContainerW = 327.f;
static const CGFloat kKSTitleW = 189.f;

static const CGFloat kKSTitleLabTop = 27.f;
static const CGFloat kKSMiddleMargin = 9.f;

@implementation HKMainPageCellViewModel

@end

@interface HKMainPageCell ()

@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *subTitleLab;

@end

@implementation HKMainPageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.bgImageView];
        [self.bgImageView addSubview:self.titleLab];
        [self.bgImageView addSubview:self.subTitleLab];
        
        CGFloat containerW = [[self class] containerRealW];
        CGFloat containerH = [[self class] containerRealH];
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kKSTopMargin);
            make.width.mas_equalTo(containerW);
            make.height.mas_equalTo(containerH);
            make.centerX.mas_equalTo(self.contentView);
        }];
        
        CGFloat heightRate = containerH / kKSContainerH;
        CGFloat widthRate = containerW / kKSContainerW;
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kKSTitleLabTop * heightRate);
            make.width.mas_equalTo( kKSTitleW * widthRate);
            make.left.mas_equalTo(12.f);
        }];
        
        [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLab.mas_bottom).offset(kKSMiddleMargin *heightRate);
            make.width.mas_equalTo(self.titleLab);
            make.left.mas_equalTo(self.titleLab);
        }];
    }
    return self;
}

- (void)updateContent:(id)content {
    HKMainPageCellViewModel *viewModel = KSSAFE_CAST(content, HKMainPageCellViewModel);
    self.bgImageView.image = [UIImage imageNamed:viewModel.imageName];
    self.titleLab.text = viewModel.title;
    self.subTitleLab.text = viewModel.subTitle;
}

+ (CGFloat)cellHeightWithContent:(id)content {
    return kKSTopMargin + [self containerRealH] + kKSBottomMargin;
}

+ (CGFloat)containerRealH {
    CGFloat realWidth = [self containerRealW];
    CGFloat realH = realWidth / [self widthAndHeightRate];
    return realH;
}

+ (CGFloat)containerRealW {
    return HKScreenWidth - KSLeftMargin - KSRightMargin;
}

+ (CGFloat)widthAndHeightRate {
    return kKSContainerW / kKSContainerH;
}


#pragma mark - getter & setter
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [UIImageView new];
    }
    return _bgImageView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel labelWithFont:[HKFontHelper appcnBoldFontWithSize:24.f]
                                    bgColor:nil
                                  textColor:UIColorFromRGB(0xFFFFFF) textAligment:NSTextAlignmentLeft];
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel labelWithFont:[HKFontHelper appcnFontWithSize:13.f]
                                    bgColor:nil
                                  textColor:UIColorFromRGB(0xffffff) textAligment:NSTextAlignmentLeft];
        _subTitleLab.numberOfLines = 2;
    }
    return _subTitleLab;
}

@end
